package com.mars.foodserver.service.impl;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.mars.foodserver.config.OssProperties;
import com.mars.foodserver.exception.WorkException;
import com.mars.foodserver.service.IUploadService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
 * @author wq
 * @date 2020-09-03
 */
@Service
public class UploadServiceImpl implements IUploadService {

    private OSS ossClient;

    private OssProperties ossProperties;

    private final String[] types = (String[]) Arrays.asList("jpg", "jpeg", "png", "JPG", "gif", "GIF", "bmp", "WebP", "BPM").toArray();

    public UploadServiceImpl(OssProperties ossProperties) {
        this.ossProperties = ossProperties;
        ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(),
                ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
    }


    @Override
    public String uploadImage(MultipartFile multipartFile) throws IOException {
        if (multipartFile == null) {
            throw new WorkException("请上传文件");
        }
        String originalFilename = multipartFile.getOriginalFilename();
        if (StringUtils.isEmpty(originalFilename)) {
            throw new WorkException("文件名不能为空");
        }
        long size = multipartFile.getSize();
        if (size > 2 * 1024 * 1024) {
            throw new WorkException("文件大小不能大于 2M");
        }
        String type = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        if (!arrayContain(type, types)) {
            throw new WorkException("请上传图片");
        }
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
        String fileName = "img/" + System.currentTimeMillis() + "." + type;
        // 拼接图片的宽高
        String s = calWidthAndHeight(multipartFile.getInputStream());
        ossClient.putObject(ossProperties.getBucketName(), fileName, multipartFile.getInputStream());
        String filepath = ossProperties.getPrefix() + "/" + fileName + "?" + s;
        return filepath;
    }


    private Boolean arrayContain(String target, String[] source) {
        for (String s : source) {
            if (s.equals(target)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 计算图片的宽高
     *
     * @param inputStream 流
     * @return 宽和高
     * @throws IOException 文件异常
     */
    private String calWidthAndHeight(InputStream inputStream) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(inputStream);
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();
        return "size=" + width + "x" + height;
    }


}
